package com.tc.dispatch.rwservice.test;

import com.tc.dispatch.rwservice.ejb.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import com.tc.dispatch.rwservice.model.Plan;
import com.tc.dispatch.rwservice.model.Load;
import com.tc.dispatch.rwservice.model.ERPPlanCtrl;

public class RWServiceTestClient1 extends Object {
  private static final String ERROR_NULL_REMOTE = "Remote interface reference is null.  It must be created by calling one of the Home interface methods first.";
  private static final int MAX_OUTPUT_LINE_LENGTH = 100;
  private boolean logging = true;
  private RWServiceHome rWServiceHome = null;
  private RWService rWService = null;

  //Construct the EJB test client
  public RWServiceTestClient1() {
    initialize();
    create();
  //  ERPPlanCtrl erp=new ERPPlanCtrl("sdfs","1sj01","123","sa",1,1,1,0,0,'1');
  this.findERPTaskByPages(0,10);
    //this.createERPPlanCtrl(erp);
  }

  public void initialize() {
    long startTime = 0;
    if (logging) {
      log("Initializing bean access.");
      startTime = System.currentTimeMillis();
    }

    try {
      //get naming context
      Context context = new InitialContext();

      //look up jndi name
      Object ref = context.lookup("RWService");
      //look up jndi name and cast to Home interface
      rWServiceHome = (RWServiceHome) PortableRemoteObject.narrow(ref, RWServiceHome.class);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded initializing bean access through Home interface.");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed initializing bean access.");
      }
      e.printStackTrace();
    }
  }

  //----------------------------------------------------------------------------
  // Methods that use Home interface methods to generate a Remote interface reference
  //----------------------------------------------------------------------------

  public RWService create() {
    long startTime = 0;
    if (logging) {
      log("Calling create()");
      startTime = System.currentTimeMillis();
    }
    try {
      rWService = rWServiceHome.create();
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: create()");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: create()");
      }
      e.printStackTrace();
    }

    if (logging) {
      log("Return value from create(): " + rWService + ".");
    }
    return rWService;
  }

  //----------------------------------------------------------------------------
  // Methods that use Remote interface methods to access data through the bean
  //----------------------------------------------------------------------------

  public List findWHByCP(String ssign, String spec, String plate, float qty) {
    List returnValue = null;

    if (rWService == null) {
      System.out.println("Error in findWHByCP(): " + ERROR_NULL_REMOTE);
      return returnValue;
    }

    long startTime = 0;
    if (logging) {
      log("Calling findWHByCP(" + ssign + ", " + spec + ", " + plate + ", " + qty + ")");
      startTime = System.currentTimeMillis();
    }

    try {
      returnValue = rWService.findWHByCP(ssign, spec, plate, qty);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: findWHByCP(" + ssign + ", " + spec + ", " + plate + ", " + qty + ")");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: findWHByCP(" + ssign + ", " + spec + ", " + plate + ", " + qty + ")");
      }
      e.printStackTrace();
    }

    if (logging) {
      log("Return value from findWHByCP(" + ssign + ", " + spec + ", " + plate + ", " + qty + "): " + returnValue + ".");
    }
    return returnValue;
  }

  public List findWHByBP(String ssign, String spec, float qty) {
    List returnValue = null;

    if (rWService == null) {
      System.out.println("Error in findWHByBP(): " + ERROR_NULL_REMOTE);
      return returnValue;
    }

    long startTime = 0;
    if (logging) {
      log("Calling findWHByBP(" + ssign + ", " + spec + ", " + qty + ")");
      startTime = System.currentTimeMillis();
    }

    try {
      returnValue = rWService.findWHByBP(ssign, spec, qty);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: findWHByBP(" + ssign + ", " + spec + ", " + qty + ")");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: findWHByBP(" + ssign + ", " + spec + ", " + qty + ")");
      }
      e.printStackTrace();
    }

    if (logging) {
      log("Return value from findWHByBP(" + ssign + ", " + spec + ", " + qty + "): " + returnValue + ".");
    }
    return returnValue;
  }

  public List findWHByYZ(String ssign, String spec, float qty) {
    List returnValue = null;

    if (rWService == null) {
      System.out.println("Error in findWHByYZ(): " + ERROR_NULL_REMOTE);
      return returnValue;
    }

    long startTime = 0;
    if (logging) {
      log("Calling findWHByYZ(" + ssign + ", " + spec + ", " + qty + ")");
      startTime = System.currentTimeMillis();
    }

    try {
      returnValue = rWService.findWHByYZ(ssign, spec, qty);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: findWHByYZ(" + ssign + ", " + spec + ", " + qty + ")");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: findWHByYZ(" + ssign + ", " + spec + ", " + qty + ")");
      }
      e.printStackTrace();
    }

    if (logging) {
      log("Return value from findWHByYZ(" + ssign + ", " + spec + ", " + qty + "): " + returnValue + ".");
    }
    return returnValue;
  }

  public List showPlan(String facname) {
    List returnValue = null;

    if (rWService == null) {
      System.out.println("Error in showPlan(): " + ERROR_NULL_REMOTE);
      return returnValue;
    }

    long startTime = 0;
    if (logging) {
      log("Calling showPlan(" + facname + ")");
      startTime = System.currentTimeMillis();
    }

    try {
      returnValue = rWService.showPlan(facname);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: showPlan(" + facname + ")");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: showPlan(" + facname + ")");
      }
      e.printStackTrace();
    }

    if (logging) {
      log("Return value from showPlan(" + facname + "): " + returnValue + ".");
    }
    return returnValue;
  }

  public HashMap showplanAll() {
    HashMap returnValue = null;

    if (rWService == null) {
      System.out.println("Error in showplanAll(): " + ERROR_NULL_REMOTE);
      return returnValue;
    }

    long startTime = 0;
    if (logging) {
      log("Calling showplanAll()");
      startTime = System.currentTimeMillis();
    }

    try {
      returnValue = rWService.showplanAll();
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: showplanAll()");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: showplanAll()");
      }
      e.printStackTrace();
    }

    if (logging) {
      log("Return value from showplanAll(): " + returnValue + ".");
    }
    return returnValue;
  }

  public float getLoadByNow(int fac, Date now) {
    float returnValue = 0f;

    if (rWService == null) {
      System.out.println("Error in getLoadByNow(): " + ERROR_NULL_REMOTE);
      return returnValue;
    }

    long startTime = 0;
    if (logging) {
      log("Calling getLoadByNow(" + fac + ", " + now + ")");
      startTime = System.currentTimeMillis();
    }

    try {
      returnValue = rWService.getLoadByNow(fac, now);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: getLoadByNow(" + fac + ", " + now + ")");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: getLoadByNow(" + fac + ", " + now + ")");
      }
      e.printStackTrace();
    }

    if (logging) {
      log("Return value from getLoadByNow(" + fac + ", " + now + "): " + returnValue + ".");
    }
    return returnValue;
  }

  public List getUndoPlan() {
    List returnValue = null;

    if (rWService == null) {
      System.out.println("Error in getUndoPlan(): " + ERROR_NULL_REMOTE);
      return returnValue;
    }

    long startTime = 0;
    if (logging) {
      log("Calling getUndoPlan()");
      startTime = System.currentTimeMillis();
    }

    try {
      returnValue = rWService.getUndoPlan();
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: getUndoPlan()");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: getUndoPlan()");
      }
      e.printStackTrace();
    }

    if (logging) {
      log("Return value from getUndoPlan(): " + returnValue + ".");
    }
    return returnValue;
  }

  public Plan findPlan(String salesid, String plannum) {
    Plan returnValue = null;

    if (rWService == null) {
      System.out.println("Error in findPlan(): " + ERROR_NULL_REMOTE);
      return returnValue;
    }

    long startTime = 0;
    if (logging) {
      log("Calling findPlan(" + salesid + ", " + plannum + ")");
      startTime = System.currentTimeMillis();
    }

    try {
      returnValue = rWService.findPlan(salesid, plannum);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: findPlan(" + salesid + ", " + plannum + ")");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: findPlan(" + salesid + ", " + plannum + ")");
      }
      e.printStackTrace();
    }

    if (logging) {
      log("Return value from findPlan(" + salesid + ", " + plannum + "): " + returnValue + ".");
    }
    return returnValue;
  }

  public void refreshWh() {
    if (rWService == null) {
      System.out.println("Error in refreshWh(): " + ERROR_NULL_REMOTE);
      return ;
    }

    long startTime = 0;
    if (logging) {
      log("Calling refreshWh()");
      startTime = System.currentTimeMillis();
    }

    try {
      rWService.refreshWh();
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: refreshWh()");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: refreshWh()");
      }
      e.printStackTrace();
    }
  }

  public Date getRefreshTime() {
    Date returnValue = null;

    if (rWService == null) {
      System.out.println("Error in getRefreshTime(): " + ERROR_NULL_REMOTE);
      return returnValue;
    }

    long startTime = 0;
    if (logging) {
      log("Calling getRefreshTime()");
      startTime = System.currentTimeMillis();
    }

    try {
      returnValue = rWService.getRefreshTime();
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: getRefreshTime()");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: getRefreshTime()");
      }
      e.printStackTrace();
    }

    if (logging) {
      log("Return value from getRefreshTime(): " + returnValue + ".");
    }
    return returnValue;
  }

  public int createLoad(Load load) {
    int returnValue = 0;

    if (rWService == null) {
      System.out.println("Error in createLoad(): " + ERROR_NULL_REMOTE);
      return returnValue;
    }

    long startTime = 0;
    if (logging) {
      log("Calling createLoad(" + load + ")");
      startTime = System.currentTimeMillis();
    }

    try {
      returnValue = rWService.createLoad(load);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: createLoad(" + load + ")");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: createLoad(" + load + ")");
      }
      e.printStackTrace();
    }

    if (logging) {
      log("Return value from createLoad(" + load + "): " + returnValue + ".");
    }
    return returnValue;
  }

  public void updateLoad(Load load) {
    if (rWService == null) {
      System.out.println("Error in updateLoad(): " + ERROR_NULL_REMOTE);
      return ;
    }

    long startTime = 0;
    if (logging) {
      log("Calling updateLoad(" + load + ")");
      startTime = System.currentTimeMillis();
    }

    try {
      rWService.updateLoad(load);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: updateLoad(" + load + ")");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: updateLoad(" + load + ")");
      }
      e.printStackTrace();
    }
  }

  public void removeLoad(int fac) {
    if (rWService == null) {
      System.out.println("Error in removeLoad(): " + ERROR_NULL_REMOTE);
      return ;
    }

    long startTime = 0;
    if (logging) {
      log("Calling removeLoad(" + fac + ")");
      startTime = System.currentTimeMillis();
    }

    try {
      rWService.removeLoad(fac);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: removeLoad(" + fac + ")");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: removeLoad(" + fac + ")");
      }
      e.printStackTrace();
    }
  }

  public Load getLoad(int fac) {
    Load returnValue = null;

    if (rWService == null) {
      System.out.println("Error in getLoad(): " + ERROR_NULL_REMOTE);
      return returnValue;
    }

    long startTime = 0;
    if (logging) {
      log("Calling getLoad(" + fac + ")");
      startTime = System.currentTimeMillis();
    }

    try {
      returnValue = rWService.getLoad(fac);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: getLoad(" + fac + ")");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: getLoad(" + fac + ")");
      }
      e.printStackTrace();
    }

    if (logging) {
      log("Return value from getLoad(" + fac + "): " + returnValue + ".");
    }
    return returnValue;
  }

  public List findLoad(String wherestr) {
    List returnValue = null;

    if (rWService == null) {
      System.out.println("Error in findLoad(): " + ERROR_NULL_REMOTE);
      return returnValue;
    }

    long startTime = 0;
    if (logging) {
      log("Calling findLoad(" + wherestr + ")");
      startTime = System.currentTimeMillis();
    }

    try {
      returnValue = rWService.findLoad(wherestr);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: findLoad(" + wherestr + ")");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: findLoad(" + wherestr + ")");
      }
      e.printStackTrace();
    }

    if (logging) {
      log("Return value from findLoad(" + wherestr + "): " + returnValue + ".");
    }
    return returnValue;
  }

  public void initLoad() {
    if (rWService == null) {
      System.out.println("Error in initLoad(): " + ERROR_NULL_REMOTE);
      return ;
    }

    long startTime = 0;
    if (logging) {
      log("Calling initLoad()");
      startTime = System.currentTimeMillis();
    }

    try {
      rWService.initLoad();
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: initLoad()");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: initLoad()");
      }
      e.printStackTrace();
    }
  }

  public void addData(String whid, String type, String ssign, String spec, String plate, float qty) {
    if (rWService == null) {
      System.out.println("Error in addData(): " + ERROR_NULL_REMOTE);
      return ;
    }

    long startTime = 0;
    if (logging) {
      log("Calling addData(" + whid + ", " + type + ", " + ssign + ", " + spec + ", " + plate + ", " + qty + ")");
      startTime = System.currentTimeMillis();
    }

    try {
      rWService.addData(whid, type, ssign, spec, plate, qty);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: addData(" + whid + ", " + type + ", " + ssign + ", " + spec + ", " + plate + ", " + qty + ")");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: addData(" + whid + ", " + type + ", " + ssign + ", " + spec + ", " + plate + ", " + qty + ")");
      }
      e.printStackTrace();
    }
  }

  public List findERPTask(String whereStr) {
    List returnValue = null;

    if (rWService == null) {
      System.out.println("Error in findERPTask(): " + ERROR_NULL_REMOTE);
      return returnValue;
    }

    long startTime = 0;
    if (logging) {
      log("Calling findERPTask(" + whereStr + ")");
      startTime = System.currentTimeMillis();
    }

    try {
      returnValue = rWService.findERPTask(whereStr);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: findERPTask(" + whereStr + ")");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: findERPTask(" + whereStr + ")");
      }
      e.printStackTrace();
    }

    if (logging) {
      log("Return value from findERPTask(" + whereStr + "): " + returnValue + ".");
    }
    return returnValue;
  }

  public List findERPTaskByPages(int start, int limit) {
    List returnValue = null;

    if (rWService == null) {
      System.out.println("Error in findERPTaskByPages(): " + ERROR_NULL_REMOTE);
      return returnValue;
    }

    long startTime = 0;
    if (logging) {
      log("Calling findERPTaskByPages(" + start + ", " + limit + ")");
      startTime = System.currentTimeMillis();
    }

    try {
      returnValue = rWService.findERPTaskByPages(start, limit);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: findERPTaskByPages(" + start + ", " + limit + ")");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: findERPTaskByPages(" + start + ", " + limit + ")");
      }
      e.printStackTrace();
    }

    if (logging) {
      log("Return value from findERPTaskByPages(" + start + ", " + limit + "): " + returnValue + ".");
    }
    return returnValue;
  }

  public int getERPTaskTotalNum(String whereStr) {
    int returnValue = 0;

    if (rWService == null) {
      System.out.println("Error in getERPTaskTotalNum(): " + ERROR_NULL_REMOTE);
      return returnValue;
    }

    long startTime = 0;
    if (logging) {
      log("Calling getERPTaskTotalNum(" + whereStr + ")");
      startTime = System.currentTimeMillis();
    }

    try {
      returnValue = rWService.getERPTaskTotalNum(whereStr);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: getERPTaskTotalNum(" + whereStr + ")");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: getERPTaskTotalNum(" + whereStr + ")");
      }
      e.printStackTrace();
    }

    if (logging) {
      log("Return value from getERPTaskTotalNum(" + whereStr + "): " + returnValue + ".");
    }
    return returnValue;
  }

  public List findERPWH(String whereStr) {
    List returnValue = null;

    if (rWService == null) {
      System.out.println("Error in findERPWH(): " + ERROR_NULL_REMOTE);
      return returnValue;
    }

    long startTime = 0;
    if (logging) {
      log("Calling findERPWH(" + whereStr + ")");
      startTime = System.currentTimeMillis();
    }

    try {
      returnValue = rWService.findERPWH(whereStr);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: findERPWH(" + whereStr + ")");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: findERPWH(" + whereStr + ")");
      }
      e.printStackTrace();
    }

    if (logging) {
      log("Return value from findERPWH(" + whereStr + "): " + returnValue + ".");
    }
    return returnValue;
  }

  public List findERPFactory(String whereStr) {
    List returnValue = null;

    if (rWService == null) {
      System.out.println("Error in findERPFactory(): " + ERROR_NULL_REMOTE);
      return returnValue;
    }

    long startTime = 0;
    if (logging) {
      log("Calling findERPFactory(" + whereStr + ")");
      startTime = System.currentTimeMillis();
    }

    try {
      returnValue = rWService.findERPFactory(whereStr);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: findERPFactory(" + whereStr + ")");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: findERPFactory(" + whereStr + ")");
      }
      e.printStackTrace();
    }

    if (logging) {
      log("Return value from findERPFactory(" + whereStr + "): " + returnValue + ".");
    }
    return returnValue;
  }

  public void createERPPlanCtrl(ERPPlanCtrl erp) {
    if (rWService == null) {
      System.out.println("Error in createERPPlanCtrl(): " + ERROR_NULL_REMOTE);
      return ;
    }

    long startTime = 0;
    if (logging) {
      log("Calling createERPPlanCtrl(" + erp + ")");
      startTime = System.currentTimeMillis();
    }

    try {
      rWService.createERPPlanCtrl(erp);
      if (logging) {
	long endTime = System.currentTimeMillis();
	log("Succeeded: createERPPlanCtrl(" + erp + ")");
	log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch(Exception e) {
      if (logging) {
	log("Failed: createERPPlanCtrl(" + erp + ")");
      }
      e.printStackTrace();
    }
  }

  //----------------------------------------------------------------------------
  // Utility Methods
  //----------------------------------------------------------------------------

  private void log(String message) {
    if (message == null) {
      System.out.println("-- null");
      return ;
    }
    if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
      System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) + " ...");
    }
    else {
      System.out.println("-- " + message);
    }
  }
  //Main method

  public static void main(String[] args) {
    RWServiceTestClient1 client = new RWServiceTestClient1();
    // Use the client object to call one of the Home interface wrappers
    // above, to create a Remote interface reference to the bean.
    // If the return value is of the Remote interface type, you can use it
    // to access the remote interface methods.  You can also just use the
    // client object to call the Remote interface wrappers.
  }
}